package com.kinghood.authoritycenter.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kinghood.authoritycenter.common.model.dao.sys.SysLogDO;
import com.kinghood.authoritycenter.common.model.dto.organization.SysLogQueryReqDTO;
import com.kinghood.authoritycenter.common.model.vo.sys.QueryLogPageVO;
import com.kinghood.authoritycenter.dao.sys.SysLogMapper;
import com.kinghood.authoritycenter.service.ISysLogService;
import com.kinghood.infrastructure.api.authority.AddSysLogReqDTO;
import com.kinghood.infrastructure.util.base.IdGenUtil;
import com.kinghood.sharding.model.KingHoodPageUtil;
import com.kinghood.sharding.model.KingPageResult;
import com.kinghood.sharding.model.PageResponseVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author KingHood
 * @since 2024-01-03
 */
@Slf4j
@Service
public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLogDO> implements ISysLogService {

    @Override
    public PageResponseVO<QueryLogPageVO> queryLogPage(SysLogQueryReqDTO in) {

        Page<QueryLogPageVO> page = this.baseMapper.queryPage(KingHoodPageUtil.getPage(in, in), in);
        page.getRecords().forEach(QueryLogPageVO::handleModuleName);
        return KingPageResult.getResult(page);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Long addLog(AddSysLogReqDTO in) {

        SysLogDO one = new SysLogDO();
        BeanUtils.copyProperties(in, one);
        Date now = new Date();
        one.setCreateTime(now);
        one.setId(IdGenUtil.get().nextId());
        this.save(one);

        log.info("新增日志：{}", in);
        return one.getId();
    }
}
